package cn.niit.letuseatdemo.controller.store;

import cn.niit.letuseatdemo.entity.domain.Rating;
import cn.niit.letuseatdemo.entity.domain.User;
import cn.niit.letuseatdemo.service.RatingService;
import cn.niit.letuseatdemo.util.H5LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*/**
* @Description: 前端评论控制器
* @Author: 陆品贤
* @CreateTime： 2022/12/7 15:59
*/
@RestController
public class FrontRatingController {
    @Autowired
    private RatingService ratingService;
    /**
     * 添加商品评论
     * @param rating
     * @return
     */
    @PostMapping("/api/rating")
    public Map<String, Object> evaluate(@RequestBody Rating rating){
        User user = H5LoginUtil.getLoginUser();
        /*User user = new User();
        user.setUserId(11L);
        user.setUserName("测试");*/
        rating.setUserId(user.getUserId());
        rating.setUserName(user.getUserName());
        rating.setRatingDate(new Date());
        int i = ratingService.insertRating(rating);
        HashMap<String, Object> result = new HashMap<>();
        if(i>0){
            result.put("code",200);
            result.put("msg", "评论成功");
        }else {
            result.put("code",500);
            result.put("msg", "评论失败");
        }
        return result;
    }

    /**
     * 根据菜品id查找评论
     * @param goodId
     * @return
     */
    @GetMapping("/api/rating/{goodId}")
    public Map<String, Object>getRating(@PathVariable("goodId") Long goodId){
        List<Rating> ratings = ratingService.selectRatingByGoodsId(goodId);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data",ratings);
        return result;
    }
}
